﻿package javase.socket.client;


import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
public class ChatClient3 extends JApplet implements Runnable{
	JTextArea c_JTextArea;
	JTextField c_JTextField;
	JTextField c_JTextField1;
	JLabel c_JLabel;
	List c_List;
	JPanel c_JPanlel_1;
	JPanel c_JPanlel_2;
	JPanel c_JPanlel_3;
	JPanel c_JPanlel_4;
	JButton c_JButton_send;
	JButton c_JButton_close;
	JButton c_JButton_conn;
	JComboBox c_JComboBox;
	DataInputStream c_in;
	DataOutputStream c_out;
	Socket c_s;
	String msg="";
	public void init()
	{
		setLayout(null);
		setSize(426,266);      
		c_JTextArea=new JTextArea(10,10);
		c_JTextField=new JTextField(20);
		c_JTextField1=new JTextField(10);
		c_JLabel=new JLabel();
		c_List=new List(10);
		c_JPanlel_1=new JPanel();
		c_JPanlel_2=new JPanel();
		c_JPanlel_3=new JPanel();
		c_JPanlel_4=new JPanel();
		c_JButton_send=new JButton("发送");
		c_JButton_close=new JButton("关闭");
		c_JButton_conn=new JButton("连接");
		c_JComboBox=new JComboBox();
		c_JComboBox.addItem("大家");
		c_in=null;
		c_out=null;
		setLayout(new BorderLayout());
		c_JPanlel_1.setLayout(new GridLayout(2,0));
		c_JPanlel_2.setLayout(new FlowLayout());
		c_JPanlel_3.setLayout(new FlowLayout());
		c_JPanlel_2.add(c_JComboBox,FlowLayout.LEFT);c_JPanlel_2.add(c_JTextField);
		c_JPanlel_3.add(c_JButton_send);c_JPanlel_3.add(c_JButton_close);
		c_JPanlel_1.add(c_JPanlel_2);c_JPanlel_1.add(c_JPanlel_3);
		c_JPanlel_4.add(c_JLabel);
		c_JPanlel_4.add(c_JTextField1);
		c_JPanlel_4.add(c_JButton_conn);
		add("North",c_JPanlel_4);
		add("East",c_List);
		add("Center",c_JTextArea);
		add("South",c_JPanlel_1);
		c_JTextArea.setEditable(false);
		c_JTextField.setEditable(false);
		c_JButton_send.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				String b=c_JTextField.getText();
				c_JTextField.setText("");
				try{
					c_out.writeUTF(msg+b);
				}
				catch(IOException e1){}
			}
		});
		c_JTextField.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e)
			{
    		   if(e.getKeyCode()==10)
    		   {
    			   String b=c_JTextField.getText();
        		   c_JTextField.setText("");
        		    try{
        		        c_out.writeUTF(msg+b);
        		    }
        		    catch(IOException e1){}
    		   }
			}
		});
		c_JButton_conn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					URL url=getCodeBase();
					InetAddress inetaddr=InetAddress.getByName(url.getHost());
					c_JLabel.setText("Server:"+inetaddr+""+url.getHost()+""+url.getProtocol());
					c_s=new Socket(inetaddr,8888);
				//	c_s=new Socket("127.0.0.1",8888);
					c_in=new DataInputStream(c_s.getInputStream());
					c_out=new DataOutputStream(c_s.getOutputStream());
				}
				catch(Exception e2)
				{
					System.out.println("Error:"+e);
				}
				String name=c_JTextField1.getText();
				if(name!="")
				{
					try {
						c_out.writeUTF("#List#"+name);
						c_JTextField.setEditable(true);
						//c_JComboBox.addItem(name);
						c_List.addItem(name);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		c_JComboBox.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e)
			{
				if(e.getItem().equals("大家"))
				{
					msg="#all#";
				}
				else
				{
					msg="#p#"+e.getItem()+"#";
				}
			}
		});
		c_JButton_close.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				try 
				{
					c_in.close();
					c_out.close();
		    		c_s.close();
				} 
				catch (IOException e1) 
				{
					e1.printStackTrace();
				}
				System.exit(0);
			}
		});
		new Thread(this).start();
	}
	public void run()
	{
		try
		{
			while(true)
			{
				String msg=c_in.readUTF();
				if(msg!=null)
				{
					if(msg.substring(0,6).equals("#List#"))
					{
						c_JComboBox.addItem(msg.substring(6));
						c_List.addItem(msg.substring(6));
					}
					else
					{
						c_JTextArea.append(msg+"\n");
					}
				}
			}
		}
		catch(Exception e)
		{
			c_JTextArea.append("网络问题或服务器挂了!\n");
		}
	}
	/*public void stop()
	{
		try
		{
			c_out.writeUTF("离开");
		}
		catch(IOException e){}
	}*/
}


